package com.nidu.demo.mail.model;

import com.alibaba.cola.exception.BizException;
import com.nidu.demo.base.BaseDeletableEntity;
import com.nidu.demo.base.BaseTenantEntity;
import com.nidu.demo.common.enums.BooleanEnum;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * 邮件账号领域实体
 *
 * @author auto
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class MailAccount extends BaseTenantEntity implements BaseDeletableEntity {

    private Long id;

    /**
     * 邮箱
     */
    private String mail;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * SMTP 服务器域名
     */
    private String host;

    /**
     * SMTP 服务器端口
     */
    private Integer port;

    /**
     * 是否开启 SSL
     */
    private Boolean sslEnable;

    /**
     * 是否开启 STARTTLS
     */
    private Boolean starttlsEnable;

    /**
     * 是否删除
     */
    private Boolean deleted;

    public static MailAccount create(String mail, String username, String password, String host, Integer port, Boolean sslEnable, Boolean starttlsEnable) {
        MailAccount mailAccount = new MailAccount();
        mailAccount.setMail(mail)
                .setUsername(username)
                .setPassword(password)
                .setHost(host)
                .setPort(port)
                .setSslEnable(sslEnable)
                .setStarttlsEnable(starttlsEnable);
        return mailAccount;
    }

    public static MailAccount update(Long id, String mail, String username, String password, String host, Integer port, Boolean sslEnable, Boolean starttlsEnable) {
        MailAccount mailAccount = new MailAccount();
        mailAccount.setId(id)
                .setMail(mail)
                .setUsername(username)
                .setPassword(password)
                .setHost(host)
                .setPort(port)
                .setSslEnable(sslEnable)
                .setStarttlsEnable(starttlsEnable);
        return mailAccount;
    }

    @Override
    public void isDeleted() {
        if (BooleanEnum.TRUE.getValue().equals(this.getDeleted())) {
            throw new BizException(ErrorCodeConstants.MAIL_ACCOUNT_NOT_FOUND.getCode(), ErrorCodeConstants.MAIL_ACCOUNT_NOT_FOUND.getMessage());
        }
    }

}
